package com.shangcheng.server.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangcheng.model.domain.PmsBrand;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.server.exception.BusinessException;
import com.shangcheng.server.mapper.PmsBrandMapper;
import com.shangcheng.server.service.PmsBrandService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper,PmsBrand> implements PmsBrandService {
    @Override
    public void insert(PmsBrand brand) {
        PmsBrand one = getOne(new LambdaQueryWrapper<PmsBrand>().eq(PmsBrand::getName, brand.getName()));
        if (!ObjectUtils.isEmpty(one)){
            throw new BusinessException("该品牌名已存在");
        }
        save(brand);
    }

    @Override
    public void deleteByBatch(Integer[] ids) {
        List<Integer> objects = (List<Integer>) Convert.toList(ids);
        removeByIds(objects);
    }

    @Override
    public PageResult findPage(Integer pageNum, Integer pageSize, String keyword) {
        Page<PmsBrand> brandPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PmsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(keyword),PmsBrand::getName,keyword);
        wrapper.orderByDesc(PmsBrand::getSort);
        page(brandPage,wrapper);
        List<PmsBrand> records = brandPage.getRecords();
        if (ObjectUtils.isEmpty(brandPage)){
            return null;
        }
        return new PageResult(pageNum,pageSize, (int) brandPage.getTotal(),records);
    }

    @Override
    public List<PmsBrand> findAll() {
        List<PmsBrand> list = list();
        return list;
    }

    @Override
    public void deleteById(Integer id) {
        removeById(id);
    }

    @Override
    public void updateFactoryStatus(Integer[] ids, Integer factoryStatus) {
        List<Integer> objects = (List<Integer>) Convert.toList(ids);
        UpdateWrapper<PmsBrand> wrapper = new UpdateWrapper<>();
        wrapper.in("id",objects).set(!ObjectUtils.isEmpty(factoryStatus),"factory_status",factoryStatus);
        update(wrapper);
    }

    @Override
    public void updateBrand(PmsBrand pmsBrand) {
        PmsBrand one = getOne(new LambdaQueryWrapper<PmsBrand>().eq(PmsBrand::getName, pmsBrand.getName()));
        if (!ObjectUtils.isEmpty(one)&&!one.getId().equals(pmsBrand.getId())){
            throw new BusinessException("该品牌名已存在");
        }
        LambdaUpdateWrapper<PmsBrand> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(pmsBrand.getId()),PmsBrand::getId,pmsBrand.getId());
        update(pmsBrand,wrapper);
    }

    @Override
    public PmsBrand findBrandById(Integer id) {
        PmsBrand byId = getById(id);
        return byId;
    }

    @Override
    public void showStatus(Integer[] ids, Integer showStatus) {
        List<Integer> objects = (List<Integer>) Convert.toList(ids);
        UpdateWrapper<PmsBrand> wrapper = new UpdateWrapper<>();
        wrapper.in("id",objects).set(!ObjectUtils.isEmpty(showStatus),"show_status",showStatus);
        update(wrapper);
    }
}
